<?php
/**
 * Created By PhpStorm
 * Author: patrick wu
 * Date: 2020/9/19
 * Time: 16:51
 */
namespace App\Http\Controllers\Admin;

use App\Constant\ErrorConstant;
use App\Rules\ShopRule;
use App\Service\Admin\BrandService;
use Illuminate\Support\Facades\Validator;
use Illuminate\Http\Request;

class BrandController extends BaseController
{
    protected $service;

    public function __construct(Request $request, BrandService $service)
    {
        parent::__construct($request);
        $this->service  =   $service;
    }

    /**
     * 品牌列表页
     * @return \Illuminate\Http\JsonResponse
     */
    public function listBrand()
    {
        $validator  =   Validator::make($this->request->post(), [
            'query' =>  'array',
        ], [
            'query.array' =>  '搜索条件必须为数组'
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::ADMIN_BRAND_LIST_ERROR, $validator->errors()->first());
        }

        $page   =   (int) $this->request->input('page', 1);
        $size   =   (int) $this->request->input('page_size', 20);
        [$flag, $result]    =   $this->service->brandList($this->request->post('query', []), $page, $size);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_BRAND_LIST_ERROR, $result);
        }
    }

    /**
     * 总品牌列表
     * @return \Illuminate\Http\JsonResponse
     */
    public function totalBrand()
    {
        [$flag, $result]    =   $this->service->brandTotal();
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_BRAND_LIST_ERROR, $result);
        }
    }

    /**
     * 品牌详情页
     * @return \Illuminate\Http\JsonResponse
     */
    public function infoBrand()
    {
        $validator  =   Validator::make($this->request->input(), [
            'brand_id' =>  'required|integer'
        ], [
            'brand_id.required' =>  '品牌ID不能为空',
            'brand_id.integer'  =>  '品牌ID必须为数字'
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::ADMIN_BRAND_DELETE_ERROR, $validator->errors()->first());
        }

        $brand_id   =   (int) $this->request->input('brand_id');
        [$flag, $result]    =   $this->service->brandInfo($brand_id);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_BRAND_DELETE_ERROR, $result);
        }
    }

    /**
     * 编辑图片
     * @return \Illuminate\Http\JsonResponse
     */
    public function editBrand()
    {
        $validator  =   Validator::make($this->request->post(), [
            'brand_id'  =>  'required|integer|exists:mini_brand,id',
            'shop_id'   =>  ['required', 'integer', new ShopRule],
            'sorting'   =>  'required|integer',
            'brand_name'=>  'required|max:30',
            'brand_logo'=>  'image|max:5120',
            'brand_pic' =>  'image|max:5120',
            'brand_icon'=>  'image|max:5120',
            'brand_desc'=>  'string'
        ], [
            'brand_id.required'   =>  '品牌ID不能为空',
            'shop_id.required'    =>  '店铺ID不能为空',
            'sorting.required'    =>  '品牌权重不能为空',
            'brand_name.required' =>  '品牌名称不能为空',

            'brand_id.integer'    =>  '品牌ID必须为数字',
            'brand_id.exists'     =>  '品牌不存在',
            'shop_id.integer'     =>  '店铺ID必须为数字',
            'sorting.integer'     =>  '品牌权重必须为数字',
            'brand_name.max'      =>  '品牌名称过长',
            'brand_logo.image'    =>  '品牌LOGO只能为图片',
            'brand_pic.image'     =>  '品牌封面图只能为图片',
            'brand_icon.image'    =>  '品牌图标只能为图片',
            'brand_desc.string'   =>  '品牌描述只能为字符',

            'brand_logo.max'      =>  '品牌LOGO过大',
            'brand_pic.max'       =>  '品牌封面图过大',
            'brand_icon.max'      =>  '品牌图标过大',
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::ADMIN_BRAND_EDIT_ERROR, $validator->errors()->first());
        }

        $brand_id   =   (int) $this->request->post('brand_id');
        $params     =   $this->request->all();
        [$flag, $result]    =   $this->service->brandEdit($params,  $brand_id);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_BRAND_EDIT_ERROR, $result);
        }
    }

    /**
     * 删除品牌
     * @return \Illuminate\Http\JsonResponse
     */
    public function deleteBrand()
    {
        $validator  =   Validator::make($this->request->post(), [
            'brand_id' =>  'required|integer'
        ], [
            'brand_id.required' =>  '品牌ID不能为空',
            'brand_id.integer' =>  '品牌ID必须为数字'
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::ADMIN_BRAND_DELETE_ERROR, $validator->errors()->first());
        }

        $brand_id   =   (int) $this->request->post('brand_id');
        [$flag, $result]    =   $this->service->brandDelete( $brand_id);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_BRAND_DELETE_ERROR, $result);
        }
    }

    /**
     * 添加品牌
     * @return \Illuminate\Http\JsonResponse
     */
    public function addBrand()
    {
        $validator  =   Validator::make($this->request->all(), [
            'shop_id'   =>  ['required', 'integer', new ShopRule],
            'brand_name'=>  'required|max:30',
            'brand_logo'=>  'required|image|max:5120',
            'brand_pic' =>  'required|image|max:5120',
            'brand_icon'=>  'required|image|max:5120',
            'sorting'   =>  'required|integer',
            'brand_desc'=>  'string',
        ], [
            'shop_id.required'    =>  '店铺ID不能为空',
            'sorting.required'    =>  '品牌权重不能为空',
            'brand_name.required' =>  '品牌名称不能为空',
            'brand_logo.required' =>  '品牌LOGO不能为空',
            'brand_pic.required'  =>  '品牌封面图不能为空',
            'brand_icon.required' =>  '品牌图标不能为空',

            'shop_id.integer'     =>  '店铺ID必须为数字',
            'sorting.integer'     =>  '品牌权重必须为数字',
            'brand_name.max'      =>  '品牌名称过长',
            'brand_logo.image'    =>  '品牌LOGO只能为图片',
            'brand_pic.image'     =>  '品牌封面图只能为图片',
            'brand_icon.image'    =>  '品牌图标只能为图片',
            'brand_desc.string'   =>  '品牌描述只能为字符',

            'brand_logo.max'      =>  '品牌LOGO过大',
            'brand_pic.max'       =>  '品牌封面图过大',
            'brand_icon.max'      =>  '品牌图标过大',
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::ADMIN_BRAND_ADD_ERROR, $validator->errors()->first());
        }

        [$flag, $result]    =   $this->service->brandAdd($this->request->all());
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_BRAND_ADD_ERROR, $result);
        }
    }

}
